#include "head_s.h"

//生成盐值
int my_salt(char* str){
    const char set[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    srand(time(NULL));
    for(int i=0;i<8;i++){
        str[i] = set[rand()%(sizeof(set)-1)];
    }
    str[8] = '\0';
}

//生成哈希值
int my_hash(const char* password,const char* salt,char* hash_pass){
    char* buf = crypt(password,salt);
    sprintf(hash_pass, "%s", buf);

    if(hash_pass == NULL){
        perror("哈希失败！\n");
        return -1;
    }

    //哈希成功
    LOG_WRITE(LOG_INFO, "密码哈希成功");
    return 0;
}

//登录
int login(user_t* status,const portocol_t* portocol,int net_fd,MYSQL* mysql){
    char buf[100] = {0};

    //查询表中是否有这个用户
    sprintf(buf,"select * from user_info where name = '%s'",status->user_name);

    //执行查询语句
    mysql_query(mysql,buf);

    //获取查询的结果集
    MYSQL_RES* res = mysql_store_result(mysql);

    //查询结果有几行
    int ret = mysql_num_rows(res);

    if(res == NULL || ret == 0){
        //用户不存在
        send(net_fd,"false",5,0);
        return 0;
    }

    //用户存在，返回确认信息
    send(net_fd, "true", 4, 0);

    //取出用户这一行
    MYSQL_ROW row = mysql_fetch_row(res);

    //取出盐值
    char salt[100] = {0};
    strcpy(salt,row[2]);

    //取出hash值
    memset(buf,0,sizeof(buf));
    strcpy(buf,row[3]);

    //释放结果集
    mysql_free_result(res);

    //取出表中的盐值，和传入的密码进行hash，判断密码是否正确
    //哈希后比对表中哈希值是否正确
    char hash_pass[128] = {0};
    my_hash(status->arg,salt,hash_pass);

    if(strcmp(hash_pass,buf) == 0){
        //密码正确，登录成功
        send(net_fd,"right",5,0);
    }

    //密码错误，登录失败
    else{
        send(net_fd,"wrong",5,0);
        return 0;
    }
}

//注册
int regist(user_t* status,const portocol_t* portocol, int net_fd, MYSQL* mysql){
    char buf[100] = {0};

    //查询表中是否有这个用户
    sprintf(buf,"select * from user_info where name = '%s'",status->user_name);

    //执行查询语句
    mysql_query(mysql,buf);

    //获取查询结果
    MYSQL_RES* res = mysql_store_result(mysql);

    //查询结果有几行
    int ret = mysql_num_rows(res);

    if(res != NULL && ret != 0){
        //用户存在,注册失败，重新注册
        send(net_fd,"false",5,0);
        return -1;
    }

    //释放结果集
    mysql_free_result(res);

    memset(buf,0,sizeof(buf));

    //将用户名存入表中
    sprintf(buf,"insert into user_info set name = '%s'",status->user_name);

    //执行语句
    mysql_query(mysql,buf);


    //生成盐值
    char str[10] = {0};
    my_salt(str);
    char salt[20] = {0};
    sprintf(salt, "$6$%s$", str);
    
    //将盐值存入表中
    memset(buf,0,sizeof(buf));
    sprintf(buf,"update user_info set salt = '%s' where name = '%s'",salt,status->user_name);

    //执行语句
    mysql_query(mysql,buf);

    //生成哈希值
    char hash_pass[128] = {0};
    my_hash(status->arg,salt,hash_pass);

    //将hash值存入表中
    memset(buf,0,sizeof(buf));
    sprintf(buf,"update user_info set hash_pass = '%s' where name = '%s'",hash_pass,status->user_name);
    
    //执行语句
    mysql_query(mysql,buf);

    //返回用户注册成功
    send(net_fd,"true",4,0);


    //将用户的相关信息填入文件表中

    //找到刚刚存入的用户的id
    memset(buf,0,sizeof(buf));
    sprintf(buf,"select id from user_info where name = '%s'",status->user_name);

    //执行sql语句
    mysql_query(mysql,buf);

    //获取结果集
    res = mysql_store_result(mysql);

    //取出用户这一行
    MYSQL_ROW row = mysql_fetch_row(res);

    //将用户id类型转换
    int user_id = atoi(row[0]);

    //释放结果集
    mysql_free_result(res);

    //将用户的相关信息填入文件表中

    memset(buf,0,sizeof(buf));
    sprintf(buf,"insert into file_info values(NULL, '/', %d, -1, '/', 1, NULL, 0)",user_id);

    //执行语句
    mysql_query(mysql,buf);
}
